# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from .ConnFreeswitch import *

# Create your models here.
class SipPro(models.Model):
    name = models.CharField(u'别名(英文)',unique=True,max_length=32)
    IP = models.CharField(u'IP',unique=True,max_length=32)
    proxy = models.IntegerField(u'端口',default='5060')
    #destination_number = models.CharField(u'出局表达式',default='^0(.*)$', max_length=100)
    #effective_caller_id_number = models.CharField(u'号码变换',default='${caller_id_number}', max_length=32)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'SIP网关'
        verbose_name_plural = verbose_name

class ACL(models.Model):
    name = models.CharField(u'访问者',max_length=32)
    IP = models.CharField(u'IP',max_length=32)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'ACL'
        verbose_name_plural = verbose_name

class SipUser(models.Model):
    #toll_allow_choices = (('international','国际'),('domestic','国内'),('local','市话'))
    dn = models.IntegerField(u'号码')
    number = models.IntegerField(u'放号数',default=1)
    password = models.CharField(u'密码', default='1234',max_length=32)
    vm_password = models.CharField(u'vm-密码', blank=True,null=True,max_length=16)
    toll_allow = models.CharField(u'权限', null=True,default='domestic,international,local',blank=True,max_length=64)
    accountcode = models.CharField(u'计费号码', blank=True,null=True ,max_length=32)
    user_context = models.CharField(u'user_context' , default='default', max_length=32)
    effective_caller_id_name = models.CharField(u'用户名', blank=True,null=True,max_length=32)
    effective_caller_id_number = models.CharField(u'显示号码', blank=True,null=True,max_length=32)
    outbound_caller_id_name = models.CharField(u'出站用户名',default= '$${outbound_caller_name}', max_length=32)
    outbound_caller_id_number = models.CharField(u'出站号码',default= '$${outbound_caller_id}', max_length=32)
    callgroup = models.CharField(u'呼叫组', default ='techsupport',max_length=32)
    TIMEOUT = models.IntegerField(u'超时(秒)', default=30)
    onfail = models.CharField(u'转移条件', blank=True,null=True,default='', max_length=200)
    failnum = models.CharField(u'转移号码', blank=True,null=True,max_length=100)

    def __str__(self):
        num = self.number
        if num <= 1:
            return str(self.dn)
        else:
            return str(self.dn)+"至"+str(self.dn+num-1)

    class Meta:
        verbose_name = u'号码'
        verbose_name_plural = verbose_name

#UPLOAD_PATH_FILE="/usr/local/freeswitch/sounds/zh/cn/callie/ivr/8000"
UPLOAD_PATH_FILE="upload"
class ivr(models.Model):
    name = models.CharField(u'别名(英文)', unique=True, max_length=32)
    dn = models.IntegerField(u'ivr号码', unique=True)
    greet_long = models.FileField(upload_to=UPLOAD_PATH_FILE, max_length=100, verbose_name=u'语音文件上传')
    greet_short = models.FileField(upload_to=UPLOAD_PATH_FILE, max_length=100, verbose_name=u'未拨号提示音')
    invalid_sound = models.FileField(upload_to=UPLOAD_PATH_FILE, max_length=100, verbose_name=u'按错键提示音', null=True,
                                   blank=True)
    exit_sound = models.FileField(upload_to=UPLOAD_PATH_FILE, max_length=100, verbose_name=u'超时提示音', null=True,
                                     blank=True)
    timeout = models.IntegerField(u'超时时间(毫秒)', default=20000)
    max_failures = models.IntegerField(u'按键错误次数', default=3)
    max_timeouts = models.IntegerField(u'最大超时次数', default=3)
    inter_digit_timeout = models.IntegerField(u'按键间隔(毫秒)', default=2000)
    digit_len = models.IntegerField(u'分机长度', default=4)
    extension = models.CharField(u'分机表达式', default="/^([1-9][0-9][0-9][0-9])$/",max_length=100)
    transfer_0 = models.IntegerField(u'拨0转移', blank=True, null=True)
    transfer_1 = models.IntegerField(u'拨1转移', blank=True, null=True)
    transfer_2 = models.IntegerField(u'拨2转移', blank=True, null=True)
    transfer_3 = models.IntegerField(u'拨3转移', blank=True, null=True)
    transfer_4 = models.IntegerField(u'拨4转移', blank=True, null=True)
    transfer_5 = models.IntegerField(u'拨5转移', blank=True, null=True)
    transfer_6 = models.IntegerField(u'拨6转移', blank=True, null=True)
    transfer_7 = models.IntegerField(u'拨7转移', blank=True, null=True)
    transfer_8 = models.IntegerField(u'拨8转移', blank=True, null=True)
    transfer_9 = models.IntegerField(u'拨9转移', blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '简单IVR'
        verbose_name_plural = verbose_name

class licence(models.Model):
    class Meta:
        verbose_name = u"licence"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.Meta.verbose_name

class sysrestart(models.Model):
    class Meta:
        verbose_name = u"系统重启"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.Meta.verbose_name

class LocalExtension(models.Model):
    name = models.CharField(u'别名(英文)', unique=True, max_length=32)
    number = models.CharField(u'路由表达式', unique=True, max_length=50)
    sort = models.IntegerField(u'排序', default=99)
    def __str__(self):
        return self.name

    class Meta:
        ordering = ['sort']
        verbose_name = '局内路由'
        verbose_name_plural = verbose_name

class PublicExtension(models.Model):
    name = models.CharField(u'别名(英文)', unique=True, max_length=32)
    number = models.CharField(u'路由表达式', unique=True, max_length=50)
    transfer = models.CharField(u'转移号码', max_length=32)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '入局路由'
        verbose_name_plural = verbose_name

class CallOutExtension(models.Model):
    name = models.CharField(u'别名(英文)', unique=True, max_length=32)
    destination_number = models.CharField(u'出局表达式',unique=True,default='^0(.*)$', max_length=100)
    effective_caller_id_number = models.CharField(u'号码变换',default='${caller_id_number}', max_length=32)
    sipname = models.CharField(u'SIP别名', max_length=32)
    sort = models.IntegerField(u'排序', default=99)
    def __str__(self):
        return self.name

    class Meta:
        ordering = ['sort']
        verbose_name = '出局路由'
        verbose_name_plural = verbose_name

class group(models.Model):
    groupname = models.CharField(u'别名(英文)', unique=True, max_length=32)
    destination_number = models.CharField(u'分组表达式', unique=True, max_length=100)
    user_number =models.TextField(u"分机号码")
    TIMEOUT = models.IntegerField(u'超时(秒)', default=30)
    onfail = models.CharField(u'转移条件', blank=True, null=True, default='', max_length=200)
    failnum = models.CharField(u'转移号码', blank=True, null=True, max_length=100)
    sort = models.IntegerField(u'排序', default=99)

    def __str__(self):
        return self.groupname

    class Meta:
        ordering = ['sort']
        verbose_name = '电话分组'
        verbose_name_plural = verbose_name